import 'dart:convert';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_easyrefresh/material_footer.dart';
import 'package:flutter_easyrefresh/material_header.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:panda/base/Constant.dart';
import 'package:panda/base/SignType.dart';
import 'package:panda/entity/order_list_entity.dart';
import 'package:panda/entity/order_list_entity2.dart';
import 'package:panda/http/Api.dart';
import 'package:panda/http/HttpUtil.dart';
import 'package:panda/util/AppSaveInfoUtil.dart';
import 'package:panda/util/SignUtil.dart';

class ChildOrderListPage extends StatefulWidget {
  String id = "0";
  String type = "1";
  ChildOrderListPage({Key key, @required this.id, @required this.type})
      : super(key: key);

  @override
  _ChildOrderListPageState createState() => _ChildOrderListPageState();
}

class _ChildOrderListPageState extends State<ChildOrderListPage> {
  int page = 1;
  List<OrderListDataData2> dataList = List();
  EasyRefreshController _controller = new EasyRefreshController();
  String token = "";
  @override
  void initState() {
    super.initState();
    getString(TOKEN).then((value) {
      token = value;
      loadList();
    });
  }

  loadList() async {
    Map map = new Map();
    map['parameter'] = widget.id;
    map['token'] = token;
    map['p'] = page;
    map['type'] = widget.type;
    map['per'] = 10;
    map['sign_type'] = SignType.TEMP_LIST;
    Response<String> response =
        await HttpUtil().post(Api.TEMP_LIST, data: getSign(map));
    Map userMap = json.decode(response==null?"":response.data);
    if (userMap['code'] != 1) {
      Fluttertoast.showToast(msg: userMap['msg']);
      return;
    }
    OrderListEntity2 homeListEntity = new OrderListEntity2.fromJson(userMap);
    if (mounted)
      setState(() {
        if (page == 1) {
          dataList.clear();
          dataList.addAll(homeListEntity.data.datas2);
        } else {
          dataList.addAll(homeListEntity.data.datas2);
        }
        if (page == 1) {
          _controller.resetLoadState();
          _controller.finishRefresh();
        } else {
          _controller.finishLoad(noMore: homeListEntity.data.datas2.length == 0);
          if (homeListEntity.data.datas2.length == 0) {
            Fluttertoast.showToast(msg: "暂无更多数据");
          }
        }
      });
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: Text('订单详情',
            style: TextStyle(
                fontSize: ScreenUtil.getInstance().setSp(BIG_FONT_SIZE),
                color: Colors.black)),
        backgroundColor: Colors.white,
        brightness: Brightness.light,
        centerTitle: true,
        iconTheme: new IconThemeData(color: Colors.black),
        elevation: 0.5,
      ),
      body: new Container(
          child: new EasyRefresh(
        controller: _controller,
        header: MaterialHeader(),
        footer: MaterialFooter(),
        enableControlFinishLoad: false,
        enableControlFinishRefresh: true,
        child: ListView.separated(
            itemBuilder: _renderRow,
            separatorBuilder: (BuildContext context, int index) {
              return Container(
                color: divideColor,
                height: 10,
              );
            },
            itemCount: dataList == null ? 0 : dataList.length),
        onRefresh: () async {
          page = 1;
          loadList();
        },
        // onLoad: () async {
        //   page++;
        //   loadList();
        // },
      )),
    );
  }

  Widget _renderRow(BuildContext context, int index) {
    return new GestureDetector(
      onTap: () {
        // Navigator.of(context).push(MaterialPageRoute(builder: (context) {
        //   return new OrderDetailPage(
        //     id: dataList[index].id,
        //   );
        // }));
      },
      child: new Container(
        decoration: BoxDecoration(
            color: Colors.white, borderRadius: BorderRadius.circular(0.0)),
        padding: EdgeInsets.all(ScreenUtil.getInstance().setHeight(DP_20)),
        child: new Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Row(
              children: <Widget>[
                Expanded(
                  child: Text("订单号: " + dataList[index].order_num,
                      style: TextStyle(
                          fontSize: ScreenUtil.getInstance()
                              .setSp(NORMAL_FONT_SIZE))),
                ),
                Text(
                    dataList[index].status == "1"
                        ? '待提交'
                        : dataList[index].status == "2"
                            ? "待发货"
                            : dataList[index].status == "3" ? "待收货" : dataList[index].status == "4" ?"已完成":'待审核',
                    style: TextStyle(
                        color: mainColor,
                        fontSize:
                            ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE)))
              ],
            ),
              Text("下单时间: " + dataList[index].create_time,
                style: TextStyle(
                    fontSize: ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE-2))),
            Container(
              height: 10,
            ),
            ListView.builder(
              padding: EdgeInsets.all(0),
              physics: new NeverScrollableScrollPhysics(),
              itemBuilder: (BuildContext context, int index1) {
                return Container(
                  child: Row(
                    children: <Widget>[
                      ClipRRect(
                        borderRadius: BorderRadius.circular(2),
                        child: CachedNetworkImage(
                imageUrl:
                            Api.ImgBaseUrl + dataList[index].datas[index1].img,
                            width: ScreenUtil.getInstance().setHeight(DP_120),
                            fit: BoxFit.fill,
                            height: ScreenUtil.getInstance().setHeight(DP_120)),
                      ),
                      Container(
                        width: 10,
                      ),
                      Expanded(
                  child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Text(dataList[index].datas[index1].goods_name,
                              maxLines: 2,
                              overflow: TextOverflow.ellipsis,
                              style: TextStyle(
                                  fontSize: ScreenUtil.getInstance()
                                      .setSp(NORMAL_FONT_SIZE))),
                          Container(
                            height: 4,
                          ),
                          Container(
                            width: (MediaQuery.of(context).size.width -
                                ScreenUtil.getInstance().setHeight(DP_180)),
                            child: Html(
                                data: "规格: " +
                                    dataList[index].datas[index1].sku_str,
                                defaultTextStyle: TextStyle(
                                    color: Colors.black45,
                                    fontSize: ScreenUtil.getInstance()
                                        .setSp(SMALL_FONT_SIZE))),
                          ),
                          Container(
                            height: 6,
                          ),
                          Row(
                            children: <Widget>[
                              Text(MONEY + dataList[index].datas[index1].price,
                                  style: TextStyle(
                                      color: mainColor,
                                      fontSize: ScreenUtil.getInstance()
                                          .setSp(NORMAL_FONT_SIZE))),
                                Container(
                                width: 20,
                              ),
                              Text("X" + dataList[index].datas[index1].num,
                                  style: TextStyle(
                                      color: black_99,
                                      fontSize: ScreenUtil.getInstance()
                                          .setSp(SMALL_FONT_SIZE))),
                            ],
                          )
                        ],
                      )
                      )
                    ],
                  ),
                );
              },
              itemCount: dataList[index].datas.length,
              shrinkWrap: true,
            ),
            Container(
              height: 10,
            ),
            Text(
                "收货人: " +
                    dataList[index].consignee.toString() +
                    "   " +
                    dataList[index].contact_number.toString(),
                style: TextStyle(
                    color: black_99,
                    fontSize: ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE))),
            Container(
              width: double.infinity,
              alignment: Alignment.centerRight,
              child: Text("实际付款: " + dataList[index].allprice,
                  style: TextStyle(
                      color: black,
                      fontSize:
                          ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE))),
            ),
            Container(
              height: 10,
            ),
            // Row(
            //   mainAxisAlignment: MainAxisAlignment.end,
            //   crossAxisAlignment: CrossAxisAlignment.end,
            //   children: <Widget>[
            //     Offstage(
            //       offstage: dataList[index].status != "1",
            //       child: GestureDetector(
            //         child: Container(
            //           padding: EdgeInsets.fromLTRB(10, 2, 10, 2),
            //           decoration: BoxDecoration(
            //               border: Border.all(color: black_99, width: 1),
            //               borderRadius: BorderRadius.circular(30)),
            //           child: Text('取消订单',
            //               style: TextStyle(
            //                   fontSize: ScreenUtil.getInstance()
            //                       .setSp(SMALL_FONT_SIZE),
            //                   color: black_99)),
            //         ),
            //         onTap: () {
            //           showDialog(
            //             context: context,
            //             builder: (context) => new AlertDialog(
            //               title: new Text('提示'),
            //               content: new Text('确定取消该条订单吗，取消后不可撤回'),
            //               actions: <Widget>[
            //                 new FlatButton(
            //                   onPressed: () => Navigator.of(context).pop(false),
            //                   child: new Text(
            //                     '取消',
            //                     style: TextStyle(color: Colors.grey),
            //                   ),
            //                 ),
            //                 new FlatButton(
            //                   onPressed: () {
            //                     Navigator.of(context).pop(false);
            //                     cancle(index);
            //                   },
            //                   child: new Text('确定取消',
            //                       style: TextStyle(color: mainColor)),
            //                 ),
            //               ],
            //             ),
            //           );
            //         },
            //       ),
            //     ),
            //       Offstage(
            //       offstage: dataList[index].status != "3",
            //       child: GestureDetector(
            //         child: Container(
            //           margin: EdgeInsets.only(right: 10),
            //           padding: EdgeInsets.fromLTRB(10, 2, 10, 2),
            //           decoration: BoxDecoration(
            //               border: Border.all(color: mainColor, width: 1),
            //               borderRadius: BorderRadius.circular(30)),
            //           child: Text('查看物流',
            //               style: TextStyle(
            //                   fontSize: ScreenUtil.getInstance()
            //                       .setSp(SMALL_FONT_SIZE),
            //                   color: mainColor)),
            //         ),
            //         onTap: () {
            //           Fluttertoast.showToast(msg: "敬请期待");
            //         },
            //       ),
            //     ),
            //     Offstage(
            //       offstage: dataList[index].status != "3",
            //       child: GestureDetector(
            //         child: Container(
            //           padding: EdgeInsets.fromLTRB(10, 2, 10, 2),
            //           decoration: BoxDecoration(
            //             color: mainColor,
            //               border: Border.all(color: mainColor, width: 1),
            //               borderRadius: BorderRadius.circular(30)),
            //           child: Text('确认收货',
            //               style: TextStyle(
            //                   fontSize: ScreenUtil.getInstance()
            //                       .setSp(SMALL_FONT_SIZE),
            //                   color: white)),
            //         ),
            //         onTap: () {
            //           showDialog(
            //             context: context,
            //             builder: (context) => new AlertDialog(
            //               title: new Text('提示'),
            //               content: new Text('确定已经收到货物吗,确认后不可撤回'),
            //               actions: <Widget>[
            //                 new FlatButton(
            //                   onPressed: () => Navigator.of(context).pop(false),
            //                   child: new Text(
            //                     '取消',
            //                     style: TextStyle(color: Colors.grey),
            //                   ),
            //                 ),
            //                 new FlatButton(
            //                   onPressed: () {
            //                     Navigator.of(context).pop(false);
            //                     confirm(index);
            //                   },
            //                   child: new Text('确定',
            //                       style: TextStyle(color: mainColor)),
            //                 ),
            //               ],
            //             ),
            //           );
            //         },
            //       ),
            //     )
            //   ],
            // )
          ],
        ),
      ),
    );
  }
}
